﻿using KWT.DRG_DIP.API.Infrastructure;
using KWT.DRG_DIP.Common;
using KWT.DRG_DIP.DO.System;
using KWT.DRG_DIP.DTO.qcRules;
using KWT.DRG_DIP.PO.qcRules;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace KWT.DRG_DIP.API.Controllers.qcRules
{
    /// <summary>
    /// 质控规则字段
    /// </summary>
    [ApiExplorerSettings(GroupName = "qc")]
    [AllowAnonymous]
    public class RuleFieldController : QCControllerBase
    {
        #region 添加或编辑
        /// <summary>
        /// 添加或编辑
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public OutputResult AddOrUpdate(InRuleField dto)
        {
            if(string.IsNullOrEmpty(dto.FildNameInnert)|| string.IsNullOrEmpty(dto.FildName))
            {
                return Fail("字段名称或字段内部名称不可为空！");
            }
            var fields = _mapper.Map<qc_Rules_Fields>(dto);

            if (!ef.Set<qc_Rules_Fields>().Any(x => x.FDID == dto.FDID))
            {
                if (ef.Set<qc_Rules_Fields>().Any(x => x.FildName == dto.FildName))
                {
                    return Fail("字段名称重复！");
                }

                fields.FDID = "FD" + snow_id.CreateId().ToString();
                dto.FDID = fields.FDID;
                ef.Add(fields);
            }
            else
            {
                if (ef.Set<qc_Rules_Fields>().Any(x => x.FildName == dto.FildName && x.FDID != dto.FDID))
                {
                    return Fail("字段名称重复！");
                }

                var para = ef.Find<qc_Rules_Fields>(dto.FDID);
                if (para == null)
                {
                    return Fail("该项不存在！");
                }
                para.FildName = dto.FildName;
                para.FildType = dto.FildType;
                para.FildLocation = dto.FildLocation;
                para.FildRemark = dto.FildRemark;
                para.FildNameInnert = dto.FildNameInnert;
                ef.Update(para);
            }
            var flag = ef.SaveChanges() > 0;
            return Output(true, "操作成功", "操作失败");
        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="fdIDs"></param>
        /// <returns></returns>
        [HttpGet]
        public OutputResult Del([FromQuery] string[] fdIDs)
        {
            var flag = ef.Set<qc_Rules_Fields>().Where(x => fdIDs.Contains(x.FDID)).ExecuteDelete() > 0;
            return Output(flag, "操作成功", "操作失败");

        }
        #endregion

        #region  根据名称查询规则字段(分页)
        /// <summary>
        /// 根据名称查询规则字段(分页)
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [ProducesResponseType(typeof(Output<qc_Rules_Fields>), 200)]
        [HttpPost]
        public OutputResult PageList(InRuleFieldPage dto)
        {
            
            var q = ef.Set<qc_Rules_Fields>().AsQueryable();
            if (!string.IsNullOrEmpty(dto.FildName))
            {
                q = q.Where(x => EF.Functions.Like(x.FildName, $"%{dto.FildName}%"));
            }
            int total = q.Count();
            var list = q.ToPageList(dto.PageNo, dto.PageSize);
            return Output(true, "", "", new PageList<qc_Rules_Fields>() { List = list, Total = total });
        }
        #endregion
    }
}
